package com.yizhilu.os.image;

import com.yizhilu.os.core.util.DateUtils;
import com.yizhilu.os.core.util.web.HttpUtil;
import com.yizhilu.os.image.util.FileDownloadUtil;
import com.yizhilu.os.image.util.FileUtil;
import net.sf.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author:Steven
 * @date 2018/1/12 16:43
 */
public class TestSout {
    public static void downLoadWeixin() {
        try {
            String res = HttpUtil.doGet("http://testweb.quality-in.com/weixin/login/getAccessToken",null);
            JSONObject jsonObject = JSONObject.fromObject(res);
            Map<String,Object> result = new HashMap<>();
            result.put("status","1");
            if("1".equals(jsonObject.get("status"))){
                String mediaId = "RxZPl_INQWHXlh0ft6ZkAFdHo5nGLfDEBM6cg_5OE7VjVmc2Z7VEwSRM8huGpNtw";
                String url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token="+jsonObject.get("result")+"&media_id="+mediaId;
                StringBuilder savePath = new StringBuilder(FileUtil.rootpath);
                StringBuilder urlPath = new StringBuilder(FileUtil.pathfix);
                String fileName = FileUtil.getRandomFileNameString(".amr");
                urlPath.append(File.separator).append("sns/voiceLive")
                        .append(File.separator).append("mp3File").append(File.separator).append(DateUtils.toString(new Date(), "yyyyMMdd"));
                savePath.append(urlPath);
                File isD = new File(savePath.toString());
                if (!isD.isDirectory()) {
                    isD.mkdirs();
                }
                FileDownloadUtil.downLoadFromUrl(url,FileUtil.getRandomFileNameString(".amr"),savePath.toString());
                result.put("url","/"+urlPath.toString() );
            }else{
                result.put("status","0");
            }
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        String sourcePath = "F:\\tmp\\语音001.amr";
        String targetPath = "F:\\tmp\\语音001.mp3";
        //FileUtil.changeToMp3(sourcePath,targetPath);
        File f = new File("C:\\qualityIn\\upload\\sns\\voiceLive\\mp3File\\20180112\\1515746931006724629.amr");
        FileInputStream  fis= new FileInputStream(f);
        System.out.println(f.getUsableSpace());
    }

}
